<?php 
## include required files
/*******************************/
require_once '../../includefiles.php';
require_once '../../model/users.php';
require_once '../../model/email.php';
include_once("../../model/trade.php");
include_once("../../model/state.php");
require_once '../../model/cms.php';
require_once "../../includes/classes/paging_htacess.php";

## creating objects of required models
$userObj  = new Model_Users();
$tradeObj = new Model_TRADE();
$stateObj = new Model_State();
$cmsObj 		= new Model_CMS();

if($_SESSION['getbids_id']=='')
{	
	@header("location:".SITE_URL."/signin/");
	exit;
}
if($_SESSION['user_type'] == '4')
{
	@header("location:".SITE_URL."/leads/");
	exit;
}

/*===========================================================================*/
$userId =$_SESSION['getbids_id'];
if($userId!='')
{
	$userDetArray = $userObj->getUserDetailsByUserId($userId);
	$smarty->assign("userDetArray", $userDetArray);	
}
/*===========================================================================*/	


## for pagination
	$page = $_GET['id1'];
	if(!isset($_GET['id1']))
	$page = 1;
	else
	$page = $page;
	
## limit per pag 
$tetsimonialPerPage =10;
$StartRow = $tetsimonialPerPage  * ($page-1);
## Getting all jobs from database according to user
if($_POST['trade']!='0' && $_POST['trade']!='')
{
	$trade = $_POST['trade'];
	$getposttradedetail = $tradeObj->getTradeCategoryDetailsById($trade);
	$trade_cat_name = $getposttradedetail['trade_category'];	
} 
elseif($_GET['id2']!='' && $_GET['id2']!='trade')
{
	$trade =base64_decode($_GET['id2']); 
	$getgettradedetail = $tradeObj->getTradeCategoryDetailsById($trade);	
	$trade_cat_name = $getgettradedetail['trade_category'];
}
elseif($_GET['tradename']!='')
{

	$getnewtradedetail = $tradeObj->checkTradeCatNameExits(str_replace("-"," ",str_replace("and","&",$_GET['tradename'])));	
	$trade = $getnewtradedetail['id'];
	$trade_cat_name = $getnewtradedetail['trade_category'];
}
else 
{
	$trade = '';
}
$trade_cat_desc = $getnewtradedetail['description'];

if($_POST['stateid']!='0' && $_POST['stateid']!='')
{
	$stateid = $_POST['stateid']; 
	if($stateid!='')
	{
		$getstatedetail = $stateObj->getStateDetailsById($stateid);
		$stateid = $getstatedetail['state_id']; 
		$userstatename = $getstatedetail['state_name']; 
		
		$getallpostalcode = $stateObj->getAllSuburb($getstatedetail['iso1_code']);
					
		for($i=0;$i<count($getallpostalcode);$i++)
		{
			$getallpostalcode_arr[] ="'".$getallpostalcode[$i]["postcode"]."'";
		}
		if($getallpostalcode_arr[0]!='')
			$implodearray = @implode(",",$getallpostalcode_arr);
		else
			$implodearray = 0;	
			
		$postalcode = $implodearray;
	}
} 
elseif($_GET['id3']!='' && $_GET['id3']!='state')
{

	$stateid =base64_decode($_GET['id3']); 
	if($stateid!='')
	{
		$getstatedetail = $stateObj->getStateDetailsById($stateid);
		$stateid = $getstatedetail['state_id']; 
		$userstatename = $getstatedetail['state_name']; 
		
		$getallpostalcode = $stateObj->getAllSuburb($getstatedetail['iso1_code']);
					
		for($i=0;$i<count($getallpostalcode);$i++)
		{
			$getallpostalcode_arr[] ="'".$getallpostalcode[$i]["postcode"]."'";
		}
		if($getallpostalcode_arr[0]!='')
			$implodearray = @implode(",",$getallpostalcode_arr);
		else
			$implodearray = 0;	
			
		$postalcode = $implodearray;
		
	}
}
elseif($_GET['statename']!='')
{
		$getnewstatedetail=$stateObj->checkStateName(str_replace("-"," ",$_GET['statename']));
		$stateid =$getnewstatedetail['state_id']; 
		if($stateid!='')
		{
			$getstatedetail = $stateObj->getStateDetailsById($stateid);
			$stateid = $getstatedetail['state_id']; 
			$userstatename = $getstatedetail['state_name']; 
			
			$getallpostalcode = $stateObj->getAllSuburb($getstatedetail['iso1_code']);
						
			for($i=0;$i<count($getallpostalcode);$i++)
			{
				$getallpostalcode_arr[] ="'".$getallpostalcode[$i]["postcode"]."'";
			}
			if($getallpostalcode_arr[0]!='')
				$implodearray = @implode(",",$getallpostalcode_arr);
			else
				$implodearray = 0;	
				
			$postalcode = $implodearray;
			
		}
}
else
{
	$stateid = '';
}


if(($_GET['id2']=='' && $_GET['id3']=='') && ($_POST['trade']=='' && $_POST['stateid']=='') && ($_GET['tradename']=='' && $_GET['statename']==''))
{
	if($trade=='')
	{
		$trade = $userDetArray['tradeid']; 
	}
	if($stateid=='')
	{
		if($userDetArray['postalcode']!='')
		{
			$getpostaldetail = $stateObj->getPostalCodeDetailsByPostalId($userDetArray['postalcode']);
			$getstatedetail = $stateObj->getStateDetailsByISOCODE($getpostaldetail['state']);
			$stateid = $getstatedetail['state_id']; 
			$userstatename = $getstatedetail['state_name']; 
			$getallpostalcode = $stateObj->getAllSuburb($getstatedetail['iso1_code']);
					
			for($i=0;$i<count($getallpostalcode);$i++)
			{
				$getallpostalcode_arr[] ="'".$getallpostalcode[$i]["postcode"]."'";
			}
			if($getallpostalcode_arr[0]!='')
				$implodearray = @implode(",",$getallpostalcode_arr);
			else
				$implodearray = 0;	
				
			$postalcode = $implodearray;
		}
	}
}
$smarty -> assign("trade",$trade);
$smarty -> assign("trade_cat_name",$trade_cat_name);
$smarty -> assign("trade_cat_desc",$trade_cat_desc);
if($trade!='')
{
	$gettradedetail = $tradeObj->getTradeCategoryDetailsById($trade);	
	$smarty -> assign("gettradedetail",$gettradedetail);
}
$smarty -> assign("stateid",$stateid);
$smarty -> assign("userstatename",$userstatename);

$tradesmanArray  = $userObj->FindTradesmanAccordingToAll($trade,$postalcode,'','',false);
$totalCount        = count($tradesmanArray);
$smarty -> assign("recordsFound",$totalCount);

$result = $userObj->FindTradesmanAccordingToAll($trade,$postalcode,$tetsimonialPerPage,$StartRow,true);
$show = 1;
$total_pages = ceil($totalCount / $tetsimonialPerPage);
	if($total_pages > 1)
	$smarty -> assign("showpgnation","yes");
	$showing    = !isset($_GET["id1"]) ? 1 : $page;
	
	## it shoould be file name 
	if(($_GET['id2']!='' || $_GET['id3']!='' || $_GET['statename']!='' || $_GET['tradename']!='') || ($_POST['trade']!='' || $_POST['stateid']!='' ))
	{
		$firstlink ='';
		
		if(($_POST['trade']!='0' || $_GET['id2']!='' || $_GET['tradename']!='')&& $_GET['id2']!='trade')
			$firstlink  .=base64_encode($trade)."/";
		else	
			$firstlink  .= "trade/";

		if(($_POST['stateid']!='0' || $_GET['id3']!='' || $_GET['statename']!='')&& $_GET['id3']!='state')
			$firstlink  .= base64_encode($stateid)."/";
		else	
			$firstlink  .= "state/";
		
		
		$firstlink .= "findtradesman/";	
	}
	else
		$firstlink  = "findtradesman/";
	
	$baselink   = $firstlink; 
	if($total_pages!=0)
		$pagenation = pagination($total_pages, $show, $showing, $firstlink, $baselink, "", "findtradesman");
	$smarty -> assign("pagenation", $pagenation);
	$smarty -> assign("page", $page);
	
	for($k=0;$k<count($result);$k++)
	{
		$userreviewcount=$userObj->getUserRatingCount($result[$k]['id']);	
		$result[$k]['userreviewcount'] =$userreviewcount['cnt']	;
				
		$userAllleadcount = $userObj->getleadcountofTradesmanByUserId($result[$k]['id']);	
		if($userAllleadcount['cnt']!='0' && $userreviewcount['cnt']	!='0')
		{
				$positive =( $userreviewcount['cnt']/$userAllleadcount['cnt'] ) *100;
				$result[$k]['positive'] = number_format($positive,2);
		}
		else
			$result[$k]['positive'] = 0;
			
		$userlastreviewdetail=$userObj->getUserLastReviewDetail($result[$k]['id']);	
		if($userlastreviewdetail['job_id']!='')
		{
			$getjobdetail=$userObj->getJobDetailsById($userlastreviewdetail['job_id']);	
			$result[$k]['userlastreview_trade_category'] = $getjobdetail['trade_category'];
		}			
		$result[$k]['userlastreview_feedback'] =$userlastreviewdetail['message'];
		$result[$k]['userlastreview_username'] =$userlastreviewdetail['first_name']." ".$userlastreviewdetail['last_name'];
		$result[$k]['userlastreview_added_date'] =$userlastreviewdetail['added_date'];
		if($userlastreviewdetail['added_date']!='')
		{
			$timestamp = strtotime($userlastreviewdetail['added_date']);
		
			$result[$k]['userlastreview_added_date_suffix'] = date('S', $timestamp);
		}
	}
	//echo"<pre>";print_R($result);die();
	$smarty->assign("tradesmanArray", $result);
	

$getallTradeCat = $tradeObj->getAllTradesCategoryWithActiveStatus();
$i=0;
foreach($getallTradeCat as $newgetallTradeCat)
{
	$new_tradename =str_replace(" ","-",$newgetallTradeCat['trade_category']);	
	$getallTradeCat[$i]['new_tradename'] =str_replace("&","and",$new_tradename);	
	$i++;
}
$smarty->assign("getallTradeCat", $getallTradeCat);

$getallTradeJobType = $tradeObj->getAllJobTypeWithActiveStatus();
$smarty->assign("getallTradeJobType", $getallTradeJobType);	

$stateArray = $stateObj->getAllStateByCountryId(226);
$i=0;
foreach($stateArray as $newstateArray)
{
	$stateArray[$i]['new_statename'] =str_replace(" ","-",$newstateArray['state_name']);	
	$i++;
}

$smarty->assign("stateArray", $stateArray);	

$cmsArray = $cmsObj->getAllSubMenu();	
//echo"<pre>";print_R($cmsArray);die();
$smarty->assign('cmsArray', $cmsArray);


if($_SESSION['msg']){
		$smarty->assign("msg", $_SESSION['msg']);
		unset($_SESSION['msg']);
}

$active ="findatradesman";
$smarty->assign('active',$active);

$smarty->display(TEMPLATEDIR.'/modules/findtradesman/findtradesman.tpl');
unset($smarty);
?>
